package com.zbkj.front.controller;

import com.zbkj.dal.common.exception.CrmebException;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.dal.pojo.request.ba.MaintainRequest;
import com.zbkj.dal.pojo.response.ba.MaintainDetailResponse;
import com.zbkj.dal.pojo.response.ba.MyMaintainResponse;
import com.zbkj.dal.base.result.CommonResult;
import com.zbkj.service.service.MaintainService;
import com.zbkj.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 维修保养
 * @author tianzhonghai
 * 2023/10/10 17:41
 */
@Slf4j
@RestController
@RequestMapping("api/front/she/maintain")
@Api(tags = "维修保养")
public class SheMaintainController {
    @Autowired
    private UserService userService;
    @Autowired
    private MaintainService maintainService;

    @ApiOperation(value = "我的维修保养列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<List<MyMaintainResponse>> list(Integer status, PageParamRequest pageParamRequest) {
        return CommonResult.success(maintainService.listMyMaintain(status, pageParamRequest));
    }

    @ApiOperation(value = "提交维修保养信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Boolean> create(@Validated @RequestBody MaintainRequest maintainRequest) {
        return CommonResult.success(maintainService.create(maintainRequest));
    }

    @ApiOperation(value = "删除我的维修保养信息",notes = "只能删已提交的")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResult<Boolean> delete(@PathVariable("id") Integer id) {
        return CommonResult.success(maintainService.deleteMyMaintain(id));
    }

    @ApiOperation(value = "维修保养信息详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public CommonResult<MaintainDetailResponse> detail(@PathVariable("id")Integer id) {
        Integer userId = userService.getUserIdException();
        MaintainDetailResponse maintain = maintainService.detail(id);
        if (!ObjectUtils.nullSafeEquals(maintain.getUid(), userId)){
            throw new CrmebException("您不能查看该信息");
        }
        return CommonResult.success(maintain);
    }
}
